package apiTest;

import java.io.InputStream;
import java.nio.charset.Charset;

import info.monitorenter.cpdetector.io.ASCIIDetector;
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;
import info.monitorenter.cpdetector.io.ParsingDetector;
import info.monitorenter.cpdetector.io.UnicodeDetector;

	public class CharSetUtil {
		private static final CodepageDetectorProxy detector;
		static{
			detector=CodepageDetectorProxy.getInstance();
			detector.add(new ParsingDetector(false));
			detector.add(ASCIIDetector.getInstance());
			detector.add(UnicodeDetector.getInstance());
			detector.add(JChardetFacade.getInstance());
		}
		public static String getStreamCharSet(InputStream inputStream,String defaultCharSet){
			if(inputStream==null){
				return defaultCharSet;
			}
			int count=200;
			try{
				count=inputStream.available();
				
			}catch(Exception e){
				e.printStackTrace();
			}
			try {
				Charset charset=detector.detectCodepage(inputStream, count);
				if(charset!=null){
					return charset.name();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return defaultCharSet;
		}
	
}

